<?php
require(dirname(__FILE__) . '/includes/init.php');
if ($_REQUEST['act'] == 'read'){
	$sql = "select add_time from ".$ecs->table('page_visit')." where page_id = (select max(page_id) from ".$ecs->table('page_visit').")";
    $insert_time = $db->getOne($sql);
	if(gmtime()-$insert_time<=24*3600){
		$dao_str = "今天您已经导入过数据了";
	}else{
		require_once('includes/cls_readexcel.php'); 
		$data= new Spreadsheet_Excel_Reader(); 
		$data->setOutputEncoding('utf-8'); 
		if ($_FILES["file"]["error"] > 0)
		{
			$error = urlencode('文件出错了，错误码：'.$_FILES["file"]["error"]);
			header("Location: orders.php?error=$error\n");exit;
		}else if(end(explode(".",strtolower($_FILES["file"]['name'])))!='xls'){
			$error = urlencode('请选择excel文件');
			header("Location: orders.php?error=$error\n");exit;
		}else{
			$datapath = $_FILES["file"]["tmp_name"];
		}
		$data->read($datapath);
		$count = 0;
		for($i= 2; $i<= $data->sheets[0]['numRows']; $i++){
			$sql = "INSERT INTO " .$ecs->table('page'). " (products_sn,products_name)".
                "VALUES ('".$data->sheets[0]['cells'][$i][1]."', '".$data->sheets[0]['cells'][$i][2]."')";
			$db->query($sql);
		}
		$page_list = $db->getAll("select * from ".$ecs->table('page'));
	}
}
$tongji_list = $db->getAll("select p.*,pv.ip_address,count(pv.visit_id) as uv from ".$ecs->table('page')."as p left join ".$ecs->table('page_visit')." as pv on p.products_sn=pv.products_sn group by pv.products_sn,pv.ip_address order by pv.add_time desc");
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理中心 - 广告来源列表 </title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/general.css" rel="stylesheet" type="text/css" />
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
 
<h1>
<span class="action-span1"><a target="_top" href="index.php?act=main">管理中心</a> </span><span id="search_id" class="action-span1"> - 广告来源列表 </span>
<div style="clear:both"></div>
</h1>
<?php if(!empty($_GET['error'])){?>
<div class="form-div" id="error">
  <span><a href="#" onclick="javascript:err_hide()" title="关闭"><img src="images/no.gif" border="0" alt="关闭"></a><?php echo urldecode($_GET['error']); ?></span>
</div>
<?php }?>

<div class="form-div">
  <form method="post" action="pages.php?act=read" enctype="multipart/form-data">
   Excel<input type="file" name="file" size="50" />
   <input type="submit" name="readexcel" value="导入" class="button"/>
   <?php if($dao_str){?><span style="color:red;"><?php echo $dao_str;?></span><?php }?>
  </form>
</div>
<?php 
if(count($page_list)>0){
?>
<div class="list-div" id="page_list">
<div><a href="#" onclick="javascript:div_hide()" title="关闭"><img src="images/no.gif" border="0" alt="关闭"></a></div>
<table cellpadding="3" cellspacing="1">
  <tr>
    <th>产品ID</th>
	<th>产品名</th>
  <tr>
  <?php 
		foreach($page_list as $page){
  ?> 
  <tr>
	<td><?php echo $page['products_sn'];?></td>
	<td><?php echo $page['products_name'];?></td>
  </tr>
  <?php
   }
  ?>
</div>
<?php
}
 ?>
<div class="list-div" id="listDiv"> 
<table cellpadding="3" cellspacing="1">
  <tr>
    <th>产品ID</th>
	<th>产品名</th>
	<th>IP</th>
	<th>UV</th>
  <tr>
  <?php 
	if(count($tongji_list)>0){
		foreach($tongji_list as $ad){
  ?>
   <tr>
    <td><?php echo $ad['products_sn']?></td>
	<td><a href="http://www.yihaodian.com/product/<?php echo $ad['products_sn'];?>_784" target="_blank"><?php echo $ad['products_name'];?></a></td>
	<td><?php echo $ad['ip_address']?></td>
    <td><?php echo $ad['uv']?></td>
  <tr>
  <?php 
		}
    }
  ?>
  </table>
 
<!-- 分页 -->
  </div>
<script type="text/javascript">
function err_hide(){
	 $("#error").css("display","none"); 
}
function div_hide(){
	$("#page_list").css("display","none");
}
</script>

</body>
</html>